package com.ktjy.config;


import com.ktjy.filter.FormAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableMethodSecurity(securedEnabled = true)
public class SecurityConfig {
    @Autowired
    FormAuthenticationFilter filter;




    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        return http
                //关闭csrf
                .csrf(csrf->csrf.disable())
                //配置
                .authorizeHttpRequests(temp -> temp
                        .requestMatchers("/customer/login").permitAll()
//                        .requestMatchers("/user/**").hasRole("user")
//                        .requestMatchers("/sysRight/**").hasAuthority("sysRight:list")
                        .anyRequest().authenticated()
                )
//                .formLogin(form -> {})

//                .formLogin(form -> form
//                        .successHandler(new ScuessHander())
//                        .failureHandler(new FailHandler())
//                )

                .addFilterBefore(filter,UsernamePasswordAuthenticationFilter.class)
                //关闭session
                .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                .build();
    }


    @Bean
    public PasswordEncoder passwordEncoder() {
        //散列加密
        return new BCryptPasswordEncoder();
    }


}
